package one.stand.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import one.stand.model.ResultModel;
import one.stand.model.essay.TaskCustomerModel;
import one.stand.model.essay.TaskStaffModel;
import one.stand.request.essay.MyTaskDetailQuery;
import one.stand.request.essay.MyTaskQuery;
import one.stand.request.essay.TaskStaffRequest;
import one.stand.request.essay.UpdateMyTaskFrom;
import one.stand.response.essay.DynamicModelResponse;
import one.stand.response.essay.MyTaskResponse;
import one.stand.response.essay.MyTaskUserResponse;
import one.stand.service.BaseService;
import one.stand.service.ITaskStaffService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 任务员工 服务实现类
 * </p>
 *
 * @author luoyang
 * @since 2020-05-07
 */
@Service
public class TaskStaffServiceImpl extends BaseService implements ITaskStaffService {

    @Override
    public ResultModel<Boolean> updateMyTaskState(UpdateMyTaskFrom request) {
        //检查state是否为空
        if(request.getState() == null){
            if(this.taskStaffMapper.updateMyTaskViewState(request) > 0){
                updateTaskCustomer(request);
                return ResultModel.success();
            }
        }else if(request.getState() != null){
            if(this.taskStaffMapper.updateMyTaskState(request) > 0){
                updateTaskCustomer(request);
                return ResultModel.success();
            }
        }
        return ResultModel.fail();
    }

    private void updateTaskCustomer(UpdateMyTaskFrom request){
        //更新总任务未已查看
        TaskCustomerModel taskCustomerModel = this.taskCustomerMapper.queryById(request.getTaskCustomerId().intValue());
        if(taskCustomerModel != null){
            taskCustomerModel.setViewState(1);
            this.taskCustomerMapper.update(taskCustomerModel);
        }
    }

    @Override
    public ResultModel queryMyTask(MyTaskQuery request) {
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        //按state和 viewstate查询 如果都没有则一次查询我发布的和我收到的
        if(request.getType() == 1){
            //查询我收到的
            List<MyTaskResponse> taskStaffModels = this.taskStaffMapper.queryMyTask(request);
            if(taskStaffModels == null){
                return ResultModel.noData();
            }
            PageInfo<MyTaskResponse> pageInfo = new PageInfo(taskStaffModels);
            return ResultModel.success(pageInfo);
        }

        if(request.getType() == 2){
            //查询我发布的
            List<MyTaskResponse> taskStaffModels = this.taskStaffMapper.queryMyPubTask(request);
            if(taskStaffModels == null){
                return ResultModel.noData();
            }
            PageInfo<MyTaskResponse> pageInfo = new PageInfo(taskStaffModels);
            return ResultModel.success(pageInfo);
        }

        return ResultModel.noData();
    }

    @Override
    public ResultModel<MyTaskResponse> queryMyTaskDetail(MyTaskDetailQuery request) {
        if(request.getType() == 1){
            //查询我收到的
            MyTaskResponse taskStaffModels = this.taskStaffMapper.queryMyTaskDetail(request);
            if(taskStaffModels == null){
                return ResultModel.noData();
            }
            //任务员工 分开编写 需要分页时可扩展
            List<MyTaskUserResponse> responses = this.taskStaffMapper.queryMyTaskUser(request);
            this.transform(responses);
            taskStaffModels.setMyTask(responses);
            return ResultModel.success(taskStaffModels);
        }

        if(request.getType() == 2){
            //查询我发布的
            MyTaskResponse taskStaffModels = this.taskStaffMapper.queryMyPubTaskDetail(request);
            if(taskStaffModels == null){
                return ResultModel.noData();
            }
            //任务员工 分开编写 需要分页时可扩展
//            request.setUserId(null);
            List<MyTaskUserResponse> responses = this.taskStaffMapper.queryMyTaskUser(request);
            this.transform(responses);
            taskStaffModels.setMyTask(responses);
            //任务员工
            return ResultModel.success(taskStaffModels);
        }
        return ResultModel.noData();
    }

    @Override
    public ResultModel<Boolean> updateMyPubTaskState(UpdateMyTaskFrom request) {

        if(request.getState() != null){
            this.taskCustomerMapper.updateState(request);
            if(request.getState() == 2){
                //关闭当前任务下的所有员工任务
                List<TaskStaffModel> models = this.taskStaffMapper.queryAll(new TaskStaffRequest(){{
                    setTaskId(request.getTaskCustomerId());
                }});

                models.forEach(taskStaffModel -> {
                    taskStaffModel.setState(2);
                    this.taskStaffMapper.update(taskStaffModel);
                });
            }
            return ResultModel.success();
        }
        return ResultModel.fail();
    }

    private void transform( List<MyTaskUserResponse> responses){
        responses.forEach(myTaskUserResponse -> {
            myTaskUserResponse.setNickeName( StringUtils.isBlank(myTaskUserResponse.getNickeName()) ? "" : new String(java.util.Base64.getDecoder().decode(myTaskUserResponse.getNickeName())));
        });
    }
}
